<?php
/**
 * Created by PhpStorm.
 * User: WenQing
 * Date: 2017/6/21
 * Time: 11:04
 */
namespace App\Service;

use App\Permission;
use App\Role;

class RoleService extends BaseService
{
    /**
     * 获取所有的角色
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     * create WenQing
     */
    public function getAllRole()
    {
        return Role::all();
    }

    /**
     * 根据角色id返回角色和权限的信息
     * @param $id
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     * create WenQing
     */
    public function getRoleAndPerByRoleId($id)
    {
        $permissions = Permission::all();
        $array = [];
        $data = '';
        if ($permissions) {
            foreach ($permissions as $v) {
                array_set($array, $v->display_name, ['id' => $v->id,'name' => $v->name]);
            }
        }
        foreach ($array as $k => $v)
        {
            foreach ($v as $k2 => $v2)
            {
                $data[$k][] = $v2;
            }
        }
        $rolesData = Role::with('perms')->where('id',$id)->first()->toArray();
        $roleArray = [
            'id' => $rolesData['id'],
            'name' => $rolesData['name'],
            'display_name' => $rolesData['display_name'],
            'description' => $rolesData['description']
        ];
        $temp = [];
        foreach ($rolesData['perms'] as $k2 => $v2)
        {
            $temp[] = $v2['id'];
        }
        $roleArray['perms'] = $temp;

        return [
            'roles' => $roleArray,
            'permissions' => $data
        ];
    }
}